package com.ice.dockerredis.app;

import com.ice.dockerredis.config.Code;
import com.ice.dockerredis.config.Item;
import com.ice.dockerredis.config.SipConfiguration;
import com.ice.dockerredis.config.SubList;
import org.dom4j.Element;

import java.util.List;

/**
 *
 * 用于解析 SIP_XML 配置文件信息
 *
 * @author icewinters
 */
public class XMLSipConfigParser {

    private SipConfiguration sipConfiguration;

    public XMLSipConfigParser() {
        this.sipConfiguration = new SipConfiguration();
    }


    public SipConfiguration parse(Element rootElement) {

        parseCode(rootElement.element("Code"));
        parseFirstLevelSubList(rootElement.element("SubList"));

        return this.sipConfiguration;
    }

    private void parseFirstLevelSubList(Element element) {

        SubList subList = new SubList();

        List<Element> firstLevelItems = element.elements("Item");

        for (Element flItem : firstLevelItems) {
            Item item = parseFirstLevelItem(flItem);
            subList.addItem(item);
        }

        this.sipConfiguration.setSubList(subList);

    }

    private Item parseFirstLevelItem(Element flItem) {

        String code = flItem.attributeValue("Code");
        String name = flItem.attributeValue("Name");
        String decoderTag = flItem.attributeValue("DecoderTag");
        String longitude = flItem.attributeValue("Longitude");
        String latitude = flItem.attributeValue("Latitude");
        String subNum = flItem.attributeValue("SubNum");
        String onLine = flItem.attributeValue("OnLine");


        Element secondLevelSubList = flItem.element("SubList");

        String secondSubNum = secondLevelSubList.attributeValue("SubNum");

        SubList subList = new SubList();

        Item firstLevelItem = Item.builder().Code(code).Name(name).DecoderTag(decoderTag)
                .Longitude(longitude)
                .Latitude(latitude)
                .SubNum(subNum)
                .OnLine(onLine).subList(subList).build();

        subList.setSubNum(secondSubNum);

        List<Element> secondLevelItems = secondLevelSubList.elements("Item");

        for (Element slItem : secondLevelItems) {
            Item secondLevelItem= parseSecondLevelItem(slItem);
            subList.addItem(secondLevelItem);
        }

        return firstLevelItem;


    }

    private Item parseSecondLevelItem(Element slItem) {

        String name = slItem.attributeValue("Name");
        String code = slItem.attributeValue("Code");
        String type = slItem.attributeValue("type");
        String decoderTag = slItem.attributeValue("DecoderTag");
        String longitude = slItem.attributeValue("Longitude");
        String latitude = slItem.attributeValue("Latitude");
        String subNum = slItem.attributeValue("SubNum");
        String onLine = slItem.attributeValue("OnLine");

        String urlIr = slItem.attributeValue("url_ir");
        String videoWidth = slItem.attributeValue("videoWidth");
        String videoHeight = slItem.attributeValue("videoHeight");

        return Item.builder().Name(name).Code(code).type(type).DecoderTag(decoderTag)
                .Longitude(longitude)
                .Latitude(latitude)
                .SubNum(subNum)
                .OnLine(onLine)
                .url_ir(urlIr)
                .videoWidth(videoWidth)
                .videoHeight(videoHeight).build();

    }

    private void parseCode(Element element) {
        String name = element.attributeValue("name");
        String id = element.attributeValue("id");

        Code code = new Code();
        code.setName(name);
        code.setId(id);

        this.sipConfiguration.setCode(code);

    }
}
